<?php
/**
 *
 * 分店管理控制器
 * @author comfiger<liuqiang@wisdudu.com>
 * @version v1.0.0
 *
 **/
namespace Mama\Controller;
use Think\Controller;

class ShopController extends Controller {
	protected $shopidlist = ""; //管理的分店ID集合
	public function __construct() {
        parent::__construct();
		//判断是否登陆
		if(!session('userid')){
			$this->redirect("index/index");
		}
		//解析分店信息
		$shoplist = S('shoplist_'. session('userid'));
		$this->assign('shoplist', $shoplist);
		if(count($shoplist)) foreach($shoplist as $sp){
			$this->shopidlist .= strlen($this->shopidlist) ? ','. $sp['shop_id'] : $sp['shop_id'];
		}
		//传递参数到分页界面
		$this->assign('param', json_encode(I('get.')));
    }

    /**
     * 分店信息
     **/
    public function index(){
    	$ajax = I('get.ajax', 0);
		$page = I('get.page', 1);
		//获取姓名
		$title = I('get.title', '');
		if(!empty($title)){
			$where['title'] = array('like', '%'. $title .'%');
		}
		//获取总数
		$shopDao = D('Shop');
		$count = $shopDao->getShopCount($where);
		$this->assign('count', $count);
		//获取列表
		$shopslist = $shopDao->getShopList($where, $page);
		$this->assign('shopslist', $shopslist);
		if($ajax){
			$this->display('shop_list', 'utf-8', 'html');
		}else{
			$this->display();
		}
    }

    /**
     * 添加分店信息
     **/
    public function add(){
    	if(IS_GET){
    		$shopid = I('get.shopid', 0);
    		//获取分店详情
    		if(!empty($shopid)){
    			$shopinfo = D('Shop')->getShopInfoByShopId($shopid);
    			$this->assign('shopinfo', $shopinfo);
    		}
    		$this->display();
    	}else{
    		$data = I('post.');
    		//判断参数
    		if(!strlen($data['title'])){
    			echo 0;
    			exit;
    		}

    		if(!strlen($data['images'])){
    			echo 0;
    			exit;
    		}
    		$data['desc'] = empty($data['desc']) ? '' : $data['desc'];
    		$data['manager_id'] = session('userid');
    		$shopid = $data['shop_id'];

    		unset($data['shop_id']);
    		if($shopid){
    			$row = D('Shop')->editShopInfoByShopId($shopid, $data);
    		}else{
    			$row = D('Shop')->registerShopInfo($data);
    		}
    		if($row !== false){
    			echo 1;
    		}else{
    			echo 0;
    		}
    	}
    }

    /**
     * 小区信息
     **/
    public function village(){
    	$ajax = I('get.ajax', 0);
		$page = I('get.page', 1);
		//获取姓名
		$title = I('get.title', '');
		if(!empty($title)){
			$where['village.title'] = array('like', '%'. $title .'%');
		}
		$shopid = I('get.shopid', 0);
		if(empty($shopid)){
			$where['village.shop_id'] = array('in', $this->shopidlist);
		}else{
			$where['village.shop_id'] = $shopid;
		}
		//获取总数
		$villageDao = D('Village');
		$count = $villageDao->getVillageCount($where);
		$this->assign('count', $count);
		//获取列表
		$villagelist = $villageDao->getVillageList($where, $page);
		//echo $villageDao->_sql();
		$this->assign('villagelist', $villagelist);
		if($ajax){
			$this->display('village_list', 'utf-8', 'html');
		}else{
			$this->display();
		}
    }

    /**
     * 根据分店ID获取小区列表
     **/
    public function getvillage(){
    	$shopid = I('get.shopid', 0);
    	//查询小区列表
    	$villagelist = D('Village')->getVillageListByShopId(array(
    		'shop_id' => $shopid
    	));
    	$html = "";
    	if(count($villagelist)){
    		foreach ($villagelist as $ls) {
    			$html .= '<option value="'. $ls['villageid'] .'">'. $ls['title'] .'</option>';
    		}
    	}else{
    		$html = '<option value="0">--请选择--</option>';
    	}

    	echo $html;
    }

    /**
     * 小区添加
     **/
    public function villageadd(){
    	if(IS_GET){
    		$shopid = I('get.shopid', 0);
    		//获取分店详情
    		if(!empty($shopid)){
    			$shopinfo = D('Village')->getVillageInfoByVillageId($shopid);
    			$this->assign('shopinfo', $shopinfo);
    		}
    		$this->display('Shop/village_add');
    	}else{
    		$data = I('post.');
    		//判断参数
    		if(!strlen($data['title'])){
    			echo 0;
    			exit;
    		}

    		if(!strlen($data['images'])){
    			echo 0;
    			exit;
    		}

    		$data['desc'] = empty($data['desc']) ? '' : $data['desc'];
    		$data['admin_id'] = session('userid');
    		$data['admin_nick'] = session('nickname');
    		$data['createtime'] = time();

    		$villageid = $data['villageid'];
    		
    		unset($data['villageid']);
    		if($villageid){
    			$row = D('Village')->editVillageInfoByShopId($villageid, $data);
    		}else{
    			$row = D('Village')->registerVillageInfo($data);
    		}
    		if($row !== false){
    			echo 1;
    		}else{
    			echo 0;
    		}
    	}
    }
	
	/**
	 * 添加房间
	 **/
    public function roomadd(){
    	if(IS_GET){
    		$roomid = I('get.roomid', 0);
			//获取房间类型
			$roomDao = D('Room');
			$roomtype = $roomDao->getRoomTypeList();
			$this->assign('roomtype', $roomtype);
			//获取配套信息
			$accesslist = D('Access')->getAllAccessList(array(
				'visible' => 1
			));
			//获取房间详情
			if($roomid){
				$roominfo = $roomDao->getRoomInfoByRoomId($roomid);
				if($roominfo['images']){
					$imagelist = unserialize($roominfo['images']);
					$roominfo['images'] = $imagelist;
				}
				//遍历合同的减配信息
				$room_matching = $roominfo['matching'];
				$room_matching_attr = explode(',', $room_matching);
				foreach($accesslist as &$ls){
					if(in_array($ls['access_id'], $room_matching_attr)){
						$ls['selected'] = 1;
					}else{
						$ls['selected'] = 0;
					}
				}
				//获取小区列表
				$villagelist = D('Village')->getVillageListByShopId(array(
					'shop_id' => $roominfo['shop_id']
				));
				$this->assign('villagelist', $villagelist);
				
				$this->assign('roominfo', $roominfo);
				//查询房东信息
				if($roominfo['lease_type'] == 2){
					$landlord_profile = $roomDao->getRoomAndLandlordInfoByRoomId($roominfo['roomid']);
					//查询房东信息
					$landlord = D('Member')->getMemberInfoByUserid($landlord_profile['userid']);
					$this->assign('broominfo', $landlord_profile);
					$this->assign('userinfo', $landlord);
				}
			}
			$this->assign('accesslist', $accesslist);
    		$this->display('Shop/room_add');
    	}else{
    		$data = I('post.');
    		//判断参数
    		if(!strlen($data['doornumber'])){
    			echo 0;
    			exit;
    		}

    		if(!strlen($data['shop_id'])){
    			echo 0;
    			exit;
    		}
    		
    		if(!strlen($data['villageid'])){
    			echo 0;
    			exit;
    		}

    		$data['images'] = serialize($data['images']);
    		$data['desc'] = empty($data['desc']) ? '' : $data['desc'];
    		$data['admin_id'] = session('userid');
    		$data['admin_nick'] = session('nickname');
    		$data['createtime'] = time();
    		if($data['lease_type'] == 2){ //转租
    			$memberData = array(
    				'username' => $data['phone'],
    				'nickname' => $data['nickname'],
    				'phone' => $data['phone'],
    				'password' => md5('123456'),
    				'card' => $data['card'],
    				'roleid' => 0,
    				'admin_id' => session('userid'),
    				'admin_nick' => session('nickname')
    			);
    			//判断房东是否已经注册
    			$uinfo = D('Member')->getMemberInfoByUsername($data['phone']);
    			if(!count($uinfo)){
    				$landlordid = D('Member')->registerMemberInfo($memberData);
    			}else{
    				$landlordid = $uinfo['userid'];
    			}
    		}

    		//分离房东信息
    		$landlordData = array(
    			'userid' => $landlordid,
    			'deopsit' => $data['bdeopsit'],
    			'rent' => $data['brent'],
    			'property' => $data['bproperty'],
    			'createtime' => time()
    		);
    		unset($data['bdeopsit']);
    		unset($data['brent']);
    		unset($data['bproperty']);
    		unset($data['phone']);
    		unset($data['nickname']);
    		unset($data['card']);
    		//判断是编辑还是添加
    		$roomid = $data['roomid'];
    		$roomDao = D('Room');
    		if(empty($roomid)){
    			$row = $roomid = $roomDao->registerRoomInfo($data);
    		}else{ //编辑信息
    			$row = $roomDao->editRoomInfoByRoomId($roomid, $data);
    			if($row !== false){

    			}else{
    				echo 0;
    			}
    		}
    		//编辑房东关系
    		if($data['lease_type'] == 2){
    			$row = $roomDao->deleteRoomLandlordInfo($roomid);
    			if($row !== false){
    				$landlordData['roomid'] = $roomid;
    				$row = $roomDao->addRoomAndLandlordInfo($landlordData);
    			}
    		}

    		if($row !== false){
    			echo 1;
    		}else{
    			echo 0;
    		}
    	}
    }

    /**
     * 房间信息
     **/
    public function room(){
    	if(IS_GET){
	    	$ajax = I('get.ajax', 0);
			$page = I('get.page', 1);
			//获取小区列表
			$villagelist = D('Village')->getVillageListByShopId(array(
				'shop_id' => array('in', $this->shopidlist)
			));
			$this->assign('villagelist', $villagelist);
			//获取姓名
			$doornumber = I('get.doornumber', '');
			if(!empty($doornumber)){
				$where['room.doornumber'] = array('like', '%'. $doornumber .'%');
			}
			$shopid = I('get.shopid', 0);
			if(empty($shopid)){
				$where['room.shop_id'] = array('in', $this->shopidlist);
			}else{
				$where['room.shop_id'] = $shopid;
			}
			$villageid = I('get.villageid', 0);
			if(!empty($villageid)){
				$where['village.villageid'] = $villageid;
			}
			$where['room.isdel'] = 0;
			//获取总数
			$roomDao = D('Room');
			$count = $roomDao->getRoomCount($where);
			$this->assign('count', $count);
			//获取列表
			$roomlist = $roomDao->getRoomList($where, $page);
			$this->assign('roomlist', $roomlist);
			$this->assign('page', $page);
			if($ajax){
				$this->display('room_list', 'utf-8', 'html');
			}else{
				$this->display();
			}
		}else if(IS_POST){
			$roomid = I('post.roomid', 0);
			$row = D('Room')->editRoomInfoByRoomId($roomid, array(
				'isdel' => 1
			));

			if($row !== false){
				echo 1;
			}else{
				echo 0;
			}
		}	
    }

    /**
     * 转租房管理
     **/
    public function landlord(){
    	if(IS_GET){
	    	$ajax = I('get.ajax', 0);
			$page = I('get.page', 1);
			//获取小区列表
			$villagelist = D('Village')->getVillageListByShopId(array(
				'shop_id' => array('in', $this->shopidlist)
			));
			$this->assign('villagelist', $villagelist);
			//获取姓名
			$doornumber = I('get.doornumber', '');
			if(!empty($doornumber)){
				$where['room.doornumber'] = array('like', '%'. $doornumber .'%');
			}
			$shopid = I('get.shopid', 0);
			if(empty($shopid)){
				$where['room.shop_id'] = array('in', $this->shopidlist);
			}else{
				$where['room.shop_id'] = $shopid;
			}
			$villageid = I('get.villageid', 0);
			if(!empty($villageid)){
				$where['village.villageid'] = $villageid;
			}
			$where['room.isdel'] = 0;
			$where['room.lease_type'] = 2;
			//获取总数
			$roomDao = D('Room');
			$count = $roomDao->getLandlordRoomCount($where);
			$this->assign('count', $count);
			//获取列表
			$roomlist = $roomDao->getLandlordRoomList($where, $page);
			$this->assign('roomlist', $roomlist);
			$this->assign('page', $page);
			if($ajax){
				$this->display('landlord_list', 'utf-8', 'html');
			}else{
				$this->display();
			}
		}
    }
}
